package com.design.framework.listener;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoader;

import com.design.framework.annotion.StartRedis;
import com.design.framework.utils.JedisUtils;
import com.design.framework.utils.MyBeanUtils;

/**
 * 项目启动监听器
 * 
 * @author JohnDeng
 * @date 2018年8月8日下午3:37:06
 */
@StartRedis(connection=false)
public class WebContextListener extends ContextLoader implements ServletContextListener {
	protected Logger logger = Logger.getLogger(getClass());

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// 读取欢迎页面
		InputStream banner = this.getClass().getResourceAsStream("/template/banner.txt");
		try {
			System.out.println(new String(IOUtils.toByteArray(banner, banner.available())));
		} catch (IOException e2) {
			e2.printStackTrace();
		}
		//通过注解获取启动redis标识
		if(MyBeanUtils.isStartRedis(this)) {
			if(JedisUtils.startConnectionJedis()) {
				logger.info("启动redis成功");
			}else {
				logger.info("启动redis失败");
			}
		}
	}
}
